﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.LoadTcStudy
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.TcCommands;

[TxFeatureToggle("TC_SERVER_STUDY_LOAD_FROM_DATA_FILE")]
public class LoadTcStudy : TxButtonCommand
{
  public virtual string Category => "Prototyping";

  public virtual string Name => "Open Study by using a file containing CC and Study information";

  public virtual void Execute(object cmdParams)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    string xmlFilePath = string.Empty;
    try
    {
      xmlFilePath = this.ComputePath();
      this.LoadStudyFromTcConnectionDataFile(xmlFilePath);
    }
    catch (Exception ex)
    {
      string str = "Open connected study by file";
      int num = (int) TxMessageBox.Show($"File \"{xmlFilePath}\" could not be loaded:\n{ex.Message}", str, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    finally
    {
      Cursor.Current = current;
    }
  }

  private void LoadStudyFromTcConnectionDataFile(string xmlFilePath)
  {
    XmlDocument doc = new XmlDocument();
    doc.Load(xmlFilePath);
    this.LoadStudyByXmlData(doc);
    this.PostLoadStudy(doc);
  }

  private void LoadStudyByXmlData(XmlDocument doc)
  {
    string innerText1 = doc.SelectSingleNode("/LoadData/StudyContextUid").InnerText;
    string innerText2 = doc.SelectSingleNode("/LoadData/CollaborationContextUid").InnerText;
    TxProcessModelId txProcessModelId1 = new TxProcessModelId(innerText1);
    TxProcessModelId txProcessModelId2 = new TxProcessModelId(innerText2);
    TxStudyLoadMode txStudyLoadMode = (TxStudyLoadMode) 1;
    XmlNode xmlNode = doc.SelectSingleNode("/LoadData/StudyMode");
    if (xmlNode != null)
    {
      switch (xmlNode.InnerText)
      {
        case "Standard":
          txStudyLoadMode = (TxStudyLoadMode) 1;
          break;
        case "LineSimulation":
          txStudyLoadMode = (TxStudyLoadMode) 2;
          break;
      }
    }
    if (!(TxApplication.ActiveDocument.PlatformGlobalServicesProvider is ITxTcGlobalServicesProvider servicesProvider))
      return;
    servicesProvider.LoadStudy(txProcessModelId1, txProcessModelId2, txStudyLoadMode);
  }

  private string ComputePath()
  {
    string path = string.Empty;
    string commandLine = Environment.CommandLine;
    string str = "DnProcessSimulateCommands.TcCommands.LoadTcStudy";
    int num = commandLine.IndexOf(str);
    if (num > 0)
    {
      int startIndex = num + (str.Length + 1);
      path = commandLine.Substring(startIndex);
      if (path.IndexOf("\"") >= 0)
        path = path.Replace("\"", "");
    }
    if (path == null)
      path = Path.GetTempPath() + "tcLoadData.xml";
    return path;
  }

  private void PostLoadStudy(XmlDocument doc) => this.ExecutePostLoadCommand(doc);

  private void ExecutePostLoadCommand(XmlDocument doc)
  {
    XmlNode xmlNode = doc.SelectSingleNode("/LoadData/PostLoad/ExecuteCommand/Id");
    if (xmlNode == null)
      return;
    string innerText = xmlNode.InnerText;
    if (innerText == null)
      return;
    new TxCommandManager().ExecuteCommandByCommandProgId(innerText, (object) null, false);
  }
}
